<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14
 * Time: 14:32
 */
namespace App\Http\Controllers\Manage;

use App\Model\Designer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use OSS\OssClient;
use OSS\Core\OssException;

class DesignerController extends Controller {
    /**
     * 设计师列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function designerList() {
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
        $obj = new Designer();
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $designer = $obj->Paginate(20);
        if(empty($page)){
            $count = $obj->count();
        }

        return view('manage.designer.designerList',[
            'designer' => $designer,
            'count' => $count,
            'orderBy' => $orderBy,
            'active1'=>7,
            'active2'=>1
        ]);
    }

    public function designerAdd() {
        return view('manage.designer.designerAdd',['active1'=>7,'active2'=>2]);
    }

    public function designerCreate() {
        $rank = input::get('rank');
        $name = input::get('name');
        $description = input::get('description');
        $map = Designer::insert([
            'name' => $name,
            'rank' => $rank,
            'description' => $description,
        ]);

        if($map) {
            return [
                'code' => 100,
                'msg' => '添加成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'添加失败!'
            ];
        }


    }

    public function designerEdit($id) {
        $designer = Designer::find($id);
        if (!empty($designer)) {
            return view('manage.designer.designerEdit', ['designer' => $designer,'active1'=>7,'active2'=>1]);
        }
        else {

        }
    }

    public function designerUpdate() {
        $id = input::get('id');
        $data['rank'] = input::get('rank');
        $data['name'] = input::get('name');
        $data['description'] = input::get('description');
        if(empty($data['name']) || empty($data['description'])) {
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $update = Designer::where('id',$id)->update($data);
        if($update) {
            return [
                'code' => 100,
                'msg' => '修改成功!'
            ];
        }
        else {
            return [
                'code' => 101,
                'msg' =>'修改失败!'
            ];
        }
    }

    public function designerDelete(){
        $id = Input::get('id');
        $delete = Designer::where('id',$id)->delete();
        if($delete){
            return [
                'code'=>100,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'删除失败！'
            ];
        }
    }

    public function designerStatus() {
        $id = Input::get('id');
        $status = Input::get('status');

        if($status == 1) {
            $del = Designer::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Designer::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

}